﻿using B83.Win32;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace Jinndev {

    public class DragAndDropHelper : MonoBehaviour {

        public static DragAndDropHelper Add(GameObject gameObject, UnityAction<List<string>> action) {
            var helper = gameObject.GetComponent<DragAndDropHelper>();
            if (helper == null) {
                helper = gameObject.AddComponent<DragAndDropHelper>();
            }
            helper.onDropped.AddListener(action);
            return helper;
        }

        public static DragAndDropHelper Add(MonoBehaviour behaviour, UnityAction<List<string>> action) {
            return Add(behaviour.gameObject, action);
        }

        public UnityEvent<List<string>> onDropped = new UnityEvent<List<string>>();

        private void Awake() {
            UnityDragAndDropHook.InstallHook();
            UnityDragAndDropHook.OnDroppedFiles += OnDroppedFiles;
        }

        private void OnDestroy() {
            UnityDragAndDropHook.OnDroppedFiles -= OnDroppedFiles;
            UnityDragAndDropHook.UninstallHook();
        }

        private void OnDroppedFiles(List<string> aPathNames, POINT aDropPoint) {
            onDropped?.Invoke(aPathNames);
        }


    }

}
